package com.bwie.chenzihang.service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.bwie.chenzihang.R;


public class Service3Activity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_content;
    private Button btn_commit;
    private TextView tv_content;

    private Service3.MyBind myBind;
    private Intent intent;

    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            myBind = (Service3.MyBind) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service3);
        initView();
    }

    private void initView() {
        et_content = (EditText) findViewById(R.id.et_content);
        btn_commit = (Button) findViewById(R.id.btn_commit);
        tv_content = (TextView) findViewById(R.id.tv_content);

        btn_commit.setOnClickListener(this);
        intent = new Intent(this, Service3.class);
        startService(intent);
        bindService(intent, connection, BIND_AUTO_CREATE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_commit:
                submit();
                break;
        }
    }

    private void submit() {
        // validate
        String content = et_content.getText().toString().trim();
        String s = myBind.setText(content);
        tv_content.setText(s);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(connection);
        stopService(intent);
    }
}